<?php

namespace App\Http\Requests;

use App\Common\CommonConst;
use App\Models\PmFundPayment;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

// use App\Models\PmProject;

class PmFundPaymentRequest extends FormRequest
{
    /**
     * 表示验证器是否应在第一个规则失败时停止
     *
     * @var bool
     */
    protected $stopOnFirstFailure = true;

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     *
     * @todo 项目ID等还未对齐
     */
    public function rules(): array
    {
        return [
            // 'project_id' => [
            //     'required',
            //     'integer',
            //     Rule::exists(PmProject::class, 'id')->whereNull('deleted_at')
            // ],
            // 'sub_project_id' => [
            //     'required',
            //     'integer',
            //     Rule::exists(PmProject::class, 'id')->whereNull('deleted_at')
            // ],

            'project_id' => 'required|integer',
            'sub_project_id' => 'sometimes|required|integer',
            'payment_method' => 'required|string|min:1|max:500',
            'payment_type' => 'required|in:' . implode(',', array_keys(PmFundPayment::$paymentTypeMap)),
            'payer' => 'required|integer',
            'payee_type' => 'required|in:' . implode(',', array_keys(PmFundPayment::$payeeTypeMap)),
            'payee' => 'required|integer',
            'period' => 'required|integer|min:1|max:999',
            'individual_payment_amount' => 'required|decimal:0,2|min:0',
            'apply_payment_amount' => 'required|decimal:0,2|min:0',
            'summary' => 'required|string|max:120',
            'is_payment_together' => 'required|in:' . implode(',', array_keys(CommonConst::$statusMap)),
            'is_submit_material' => 'required|in:' . implode(',', array_keys(CommonConst::$statusMap)),
            'deadline_date' => 'required|date',
            'template_json' => 'required_if:is_submit_material,' . CommonConst::STATUS_YES . '|array',
            'template_json.*.name' => 'required|string',
            'template_json.*.file' => 'required|integer',
        ];
    }

    /**
     * 获取验证错误的自定义属性
     *
     * @return string[]
     */
    public function attributes()
    {
        return [
            'project_id' => '项目ID',
            'sub_project_id' => '子项目ID',
            'payment_method' => '付款方式',
            'payment_type' => '支出类型',
            'payer' => '付款方ID',
            'payee_type' => '收款方类型',
            'payee' => '收款方ID',
            'period' => '拨付期数',
            'individual_payment_amount' => '个体拨付金额',
            'apply_payment_amount' => '申请拨付总额',
            'summary' => '付款内容摘要',
            'is_submit_material' => '是否需要提交材料',
            'is_payment_together' => '是否同意拨付',
            'deadline_date' => '截止提交时间',
            'template_json' => '上传资料名称',
            'template_json.*.name' => '上传资料名称',
            'template_json.*.file' => '上传资料模版',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息
     *
     * @return string[]
     */
    // public function messages()
    // {
    //     return [
    //         'project_id' => '项目不能为空',
    //         'sub_project_id' => '子项目不能为空',
    //         'payment_method' => '付款方式不能为空',
    //         'payment_type' => '支出类型不能为空',
    //         'payer' => '付款方不能为空',
    //         'payee_type' => '收款方类型不能为空',
    //         'payee' => '收款方不能为空',
    //         'period' => '拨付期数不能为空',
    //         'individual_payment_amount' => '个体拨付金额不能为空',
    //         'apply_payment_amount' => '申请拨付总额不能为空',
    //         'summary' => '付款内容摘要不能为空',
    //         'is_submit_material' => '是否需要提交材料不能为空',
    //         'is_payment_together' => '是否同意拨付不能为空',
    //         'deadline_date' => '截止提交时间不能为空',
    //         'template_json' => '上传资料名称不能为空',
    //         'template_json.*.name' => '上传资料名称不能为空',
    //         'template_json.*.file' => '上传资料模版不能为空',
    //     ];
    // }
}
